/* LogBrowser.m - The log date browser.
 *
 * Copyright (C) 2008 Yuuichi Teranishi
 *
 * This file is derived from Stumbler, a program developed by iphone-wireless
 * project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * published by the Free Software Foundation.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; If not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#import <CoreFoundation/CoreFoundation.h>
#import "LogBrowser.h"
#import "LogTable.h"

@implementation LogBrowser 
- (id)initWithFrame:(struct CGRect)frame{
  if ((self == [super initWithFrame: frame]) != nil) {

    dates = [ [ NSMutableArray alloc] init ];
    delegate  = nil;

    UITableColumn *col = [ [ UITableColumn alloc ]
			   initWithTitle: @"Dates"
			   identifier:@"date"
			   width: frame.size.width
			   ];

    logTable = [ [ LogTable alloc] initWithFrame: 
				     CGRectMake(0, 0,
						frame.size.width,
						frame.size.height)];
    [ logTable addTableColumn: col ];
    [ logTable setSeparatorStyle: 1 ];
    [ logTable setDelegate: self ];
    [ logTable setDataSource: self ];
    [ self addSubview: logTable ];
    }

    return self;
}

- (void)dealloc {
  [ dates release ];
  [ logTable release ];
  [super dealloc];
}

- (void)setLogDB:(LogDB *)db {
  logdb = db;
}

- (void)reloadData {
  [ dates removeAllObjects ];
  [ logTable clearAllData ];  
  [NSThread detachNewThreadSelector:@selector(dateSearchThread)
	    toTarget:self withObject:nil];
}

- (void)dateSearchThread {
  NSAutoreleasePool* pool;
  pool = [[NSAutoreleasePool alloc]init];
  NSMutableArray *newDates = [[NSMutableArray alloc] init];
  [ logdb addDatesToArray: newDates ];
  dates = newDates;
  [ logTable performSelectorOnMainThread: @selector(reloadData)
	     withObject: nil waitUntilDone:YES ];  
  [pool release];
  [NSThread exit];   
}

- (void)setDelegate:(id)_delegate {
  delegate = _delegate;
}

- (int)numberOfRowsInTable:(UITable *)table {
  return [ dates count ];
}

- (UITableCell *)table:(UITable *)table
	    cellForRow:(int)row
		column:(UITableColumn *)col
{
  LogCell *cell = [ [ LogCell alloc ] init ];
  [ cell setTable: logTable ];
  [ cell setDates: dates ];
  [ cell setLogDB: logdb ];
  if (row <= [dates count]) {
    [ cell setTitle: [ [ dates objectAtIndex: row ]
		       descriptionWithCalendarFormat:@"%Y-%m-%d"
		       timeZone:nil
		       locale:nil
		       ]];
  }
  else {
    NSLog(@"out of bound access.");
  }
  return [ cell autorelease ];
}

- (void)tableRowSelected:(NSNotification *)notification {
  [ delegate dateSelected: [ self selectedDate ] ];
}

- (NSDate *)selectedDate {
  if ([ logTable selectedRow ] == -1)
    return nil;
  return [ dates objectAtIndex: [ logTable selectedRow ]];
}

@end
